<style scoped>
  .app-container>>>.el-tabs__nav{
    left: 50%;
    transform: translateX(-50%) !important;
  }
  .app-container>>>.el-tabs__item{
    font-size: 18px;
    color: #aaa;
  }
  .app-container>>>.is-active{
    font-weight: 600;
    color: #409EFF;
  }
</style>
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-if="formThead.merchantName" v-model="listQuery.merchantName" style="width: 200px;" class="filter-item" placeholder="商家名称" clearable @keyup.enter.native="handleFilter"/>
      <div v-if="formThead.applyWithdrawTime" class="filter-item">
        <el-date-picker v-model="listQuery.applyWithdrawTime" type="datetime" placeholder="申请提款时间" clearable @keyup.enter.native="handleFilter"/>
      </div>
      <!--<el-input v-if="formThead.state" v-model="listQuery.state" style="width: 200px;" class="filter-item" placeholder="提现状态" clearable @keyup.enter.native="handleFilter"/>-->
      <!--<div v-if="formThead.accountingTime" class="filter-item">-->
      <!--<el-date-picker v-model="listQuery.accountingTime" type="datetime" placeholder="到账时间" clearable @keyup.enter.native="handleFilter"/>-->
      <!--</div>-->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <!--<addPage @getLists="getLists"/>-->
    </div>

    <div class="filter-container">
      <el-checkbox v-model="formThead.id" label="id">id</el-checkbox>
      <el-checkbox v-model="formThead.merchantName" label="商家名称">商家名称</el-checkbox>
      <el-checkbox v-model="formThead.applyWithdrawMoney" label="申请提现金额">申请提现金额</el-checkbox>
      <el-checkbox v-model="formThead.applyWithdrawTime" label="申请提款时间">申请提款时间</el-checkbox>
      <el-checkbox v-model="formThead.nowBalance" label="申请时可提现金额">余额</el-checkbox>
      <!--<el-checkbox v-model="formThead.applySpendingMoney" label="到账金额">到账金额</el-checkbox>-->
      <!--<el-checkbox v-model="formThead.accountingTime" label="到账时间">到账时间</el-checkbox>-->
      <!--<el-checkbox v-model="formThead.accountCanWithdrawMoney" label="到账时可提现金额">到账时可提现金额</el-checkbox>-->
      <el-checkbox v-model="formThead.failureReasons" label="原因">原因</el-checkbox>
      <el-checkbox v-model="formThead.name" label="名字">名字</el-checkbox>
      <el-checkbox v-model="formThead.type" label="转账方式">转账方式</el-checkbox>
      <el-checkbox v-model="formThead.state" label="状态">状态</el-checkbox>
    </div>
    <el-tabs v-model="activeName" align="center" @tab-click="handleClick">
      <el-tab-pane label="提现记录" name="first">
        <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
          <el-table-column v-if="formThead.id" label="id" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.merchantName" label="商家名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.merchantName }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.applyWithdrawMoney" label="申请提现金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.applyWithdrawMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.applyWithdrawTime" label="申请提款时间" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.applyWithdrawTime | formatDate }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.nowBalance" label="余额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.nowBalance }}</span>
            </template>
          </el-table-column>
          <!--<el-table-column v-if="formThead.merchantExpenditure" label="到账金额" align="center">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.merchantExpenditure }}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <!--<el-table-column v-if="formThead.accountingTime" label="到账时间" align="center">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.accountingTime | formatDate }}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <!--<el-table-column v-if="formThead.accountCanWithdrawMoney" label="到账时可提现金额" align="center">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.accountCanWithdrawMoney }}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <el-table-column v-if="formThead.name" label="名字" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.type" label="转账方式" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.type }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.state" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.state === '1'">提现成功</span>
              <span v-if="scope.row.state === '2'">提现失败</span>
            </template>
          </el-table-column>
        </el-table>
        <!--页码-->
        <div class="pagination-container">
          <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
        </div>
      </el-tab-pane>
      <el-tab-pane label="提现成功" name="second">
        <el-table v-loading="listLoading" :key="tableKey" :data="list_succeed" border fit highlight-current-row style="width: 100%;">
          <el-table-column v-if="formThead.id" label="id" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.merchantName" label="商家名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.merchantName }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.applyWithdrawMoney" label="申请提现金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.applyWithdrawMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.applyWithdrawTime" label="申请提款时间" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.applyWithdrawTime | formatDate }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.nowBalance" label="余额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.nowBalance }}</span>
            </template>
          </el-table-column>
          <!--<el-table-column v-if="formThead.merchantExpenditure" label="到账金额" align="center">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.merchantExpenditure }}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <!--<el-table-column v-if="formThead.accountingTime" label="到账时间" align="center">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.accountingTime | formatDate }}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <!--<el-table-column v-if="formThead.accountCanWithdrawMoney" label="到账时可提现金额" align="center">-->
          <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.accountCanWithdrawMoney }}</span>-->
          <!--</template>-->
          <!--</el-table-column>-->
          <el-table-column v-if="formThead.name" label="名字" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.type" label="转账方式" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.type }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.state" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.state === '1'">提现成功</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="提现失败" name="third" @click="getLists">
        <el-table v-loading="listLoading" :key="tableKey" :data="list_unsuccessful" border fit highlight-current-row style="width: 100%;">
          <el-table-column v-if="formThead.id" label="id" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.merchantName" label="商家名称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.merchantName }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.applyWithdrawMoney" label="申请提现金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.applyWithdrawMoney }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.applyWithdrawTime" label="申请提款时间" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.applyWithdrawTime | formatDate }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.nowBalance" label="余额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.nowBalance }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.name" label="名字" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.failureReasons " label="失败原因" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.failureReasons }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="formThead.state" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.state === '2'">提现失败</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { get_list_with_record, del } from '@/api/merchant/merchant_withdrawal_record'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import addPage from './addPage'
import editPage from './editPage'
import { parseTime } from '@/utils/index'

export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { addPage, editPage },
  filters: {
    formatDate: function(time) {
      if (!time) {
        return ''
      }
      return parseTime(time)
    }
  },

  data() {
    return {
      activeName: 'first',
      tableKey: 0,
      list_succeed: [],
      list_unsuccessful: [],
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        id: undefined,
        applyWithdrawMoney: undefined,
        merchantName: undefined,
        applyWithdrawTime: undefined,
        merchantTotalMoney: undefined,
        state: undefined,
        accountingTime: undefined,
        bankCard: undefined,
        failureReasons: undefined,
        nowBalance: undefined,
        name: undefined,
        type: undefined,
        page: 1,
        limit: 20
      },
      formThead: {
        id: true,
        applyWithdrawMoney: true,
        merchantName: true,
        applyWithdrawTime: true,
        merchantTotalMoney: true,
        state: true,
        accountingTime: true,
        bankCard: true,
        failureReasons: true,
        nowBalance: true,
        name: true,
        type: true
      }
    }
  },
  created() {
    this.getLists()
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    getLists() {
      this.listLoading = true
      get_list_with_record(store.getters.token, this.listQuery).then(response => {
        for (let i = 0; i < Object.keys(response.data.list.data).length; i++) {
          response.data.list.data[i].applyWithdrawTime *= 1000
          response.data.list.data[i].accountingTime *= 1000
        }
        this.list = response.data.list.data
        this.list_succeed = []
        this.list_unsuccessful = []
        const len = this.list.length
        for (var i = 0; i < len; i++) {
          if (this.list[i].state === '1') {
            this.list_succeed.push(this.list[i])
            continue
          }
          if (this.list[i].state === '2') {
            this.list_unsuccessful.push(this.list[i])
            continue
          }
        }
        this.total = response.data.list.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    del(id) {
      this.$confirm('此操作将永久删除该商家提款/支出记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.info('正在删除，ID：' + id)
        del(store.getters.token, { id: id }).then(res => {
          if (res.status) {
            this.$message.success('已经删除，ID：' + id)
            this.getLists()
          } else {
            this.$message.error('删除失败，ID：' + id)
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除，ID：' + id)
      })
    }
  }
}
</script>
